package liwen.zhao.controller;

import liwen.zhao.common.jd.pojo.CommonUser;
import liwen.zhao.common.jd.utils.CookieUtils;
import liwen.zhao.common.jd.utils.MD5Util;
import liwen.zhao.common.jd.vo.SysResult;
import liwen.zhao.service.UserService;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RestController
@RequestMapping("/user")
public class FrontUserController {
    @Autowired
    UserService userService;
    @Autowired
    SysResult sysResult;
    @RequestMapping(value = "/regist",method = RequestMethod.PUT)
    public SysResult regist(CommonUser commonUser,@RequestParam(value = "psw2") String psw2, @RequestParam(value = "valicode") String valicode,  HttpServletRequest request){
        //判断验证码
        if (null == valicode || valicode.equals("")) {
            sysResult.setStatus(500);
            sysResult.setMsg("验证码不能为空");
            return sysResult;
        }
        String valiCodeId = CookieUtils.getCookieValue(request, "valiCodeId");
        String savedValiCode = userService.getValiCode(valiCodeId);
        if (!valicode.equalsIgnoreCase(savedValiCode)) {
            sysResult.setStatus(500);
            sysResult.setMsg("验证码有误");
            return sysResult;
        }
        
        //查询用户是否已经注册
        CommonUser findCommonUser=userService.getCommonUserByUserName(commonUser.getUserName());
        if(findCommonUser!=null){
            sysResult.setStatus(500);
            sysResult.setMsg("用户已存在");
            return sysResult;
        }else{
            //其它验证
            if(!commonUser.getPsw().equals(psw2)){
                sysResult.setStatus(500);
                sysResult.setMsg("两次输入的密码不一致，请重新输入");
                return sysResult;
            }
            //生成密码盐
            Integer salt= Integer.valueOf(RandomStringUtils.randomNumeric(5));
            //生成加密密码
            commonUser.setPsw( MD5Util.md5(MD5Util.md5(salt+commonUser.getPsw()+salt)));
            commonUser.setSalt(salt);
            try{
                userService.addUser(commonUser);
            }catch (Exception e){
                e.printStackTrace();
                sysResult.setStatus(500);
                sysResult.setMsg("注册失败");
                return sysResult;
            }
            return SysResult.ok();
        }
    }
    
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public SysResult login(CommonUser commonUser, @RequestParam(value = "valicode") String valicode, HttpServletRequest request, HttpServletResponse response){
        //判断验证码
        if (null == valicode || valicode.equals("")) {
            sysResult.setStatus(500);
            sysResult.setMsg("验证码不能为空");
            return sysResult;
        }
        String valiCodeId = CookieUtils.getCookieValue(request, "valiCodeId");
        String savedValiCode = userService.getValiCode(valiCodeId);
        if (!valicode.equalsIgnoreCase(savedValiCode)) {
            sysResult.setStatus(500);
            sysResult.setMsg("验证码有误");
            return sysResult;
        }
        
        //查询用户信息
        CommonUser findCommonUser=userService.getCommonUserByUserName(commonUser.getUserName());
        if(findCommonUser!=null){
            Integer salt=findCommonUser.getSalt();
            //验证密码是否正确
            if(findCommonUser.getPsw().equals(MD5Util.md5(MD5Util.md5(salt+commonUser.getPsw()+salt)))){
                String frontCookieK = userService.saveFrontUser(findCommonUser);
                if(frontCookieK!=null&&!frontCookieK.equals("")){
                    CookieUtils.setCookie(request, response, "frontCommonUser", frontCookieK);
                    sysResult.setStatus(200);
                    return sysResult;
                }else{
                    sysResult.setStatus(500);
                    sysResult.setMsg("登录失败");
                    return sysResult;
                }
               
            }else{
                sysResult.setStatus(500);
                sysResult.setMsg("用户名或密码错误");
                return sysResult;
            }
        }else{
            sysResult.setMsg("用户名或密码错误");
            return sysResult;
        }
    }
    
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
    public SysResult getUser(String cookieV) {
        SysResult sysResult = userService.getCommonUserByCookie(cookieV);
        return sysResult;
    }
    
    
}
